package alarm;

import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;

public class Alarm implements Runnable {
    private boolean isWork;
    private boolean alarmOn;
    private int alarmHour = 8;
    private int alarmMinute = 0;
    private static int nums = 0;
    private String name = "Alarm" + nums;
    AudioStream audioStream;

    public Alarm() {
        this.isWork = false;//停止响铃
        nums += 1;
        alarmOn = true;
    }

    public Alarm(int alarmHour, int alarmMinute) {
        this();
        this.alarmHour = alarmHour;
        this.alarmMinute = alarmMinute;
    }

    public Alarm(int alarmHour, int alarmMinute, String name) {
        this(alarmHour, alarmMinute);
        this.name = name;
    }

    public boolean isWork() {//获取响铃状况
        return isWork;
    }

    public void setWork(boolean isWork) {//设置响铃状况
        this.isWork = isWork;
    }

    public int getAlarmHour() {
        return alarmHour;
    }

    public void setAlarmHour(int alarmHour) {
        this.alarmHour = alarmHour;
    }

    public int getAlarmMinute() {
        return alarmMinute;
    }

    public void setAlarmMinute(int alarmMinute) {
        this.alarmMinute = alarmMinute;
    }

    public static int getNums() {
        return nums;
    }

    public static void setNums(int nums) {
        Alarm.nums = nums;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Alarm{" +
                "isWork=" + isWork +
                ", alarmTime=" + alarmHour +
                ":" + alarmMinute +
                ", name='" + name + '\'' +
                '}';
    }

    private void playAudio(String name) {
        String audioPath = "../audio/aMusic/" + name;

        try {
            InputStream input = getClass().getResourceAsStream(audioPath); //创建输入流
            if (input != null) {
                audioStream = new AudioStream(input);//用输入流input创建音频流对象
                AudioPlayer.player.start(audioStream);// 用静态成员player.start播放音乐
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    protected void startAlarm() {//设置工作方法
        synchronized (this) {
            if (isWork) {
                System.out.println("It's time for up!");
                String name = "1.wav";
                playAudio(name);
                this.notify();
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    protected void stopAlarm() {
        synchronized (this) {
            if (isWork()) {
                setWork(false);
                AudioPlayer.player.stop(audioStream);
////                System.out.println(this.name + "按了闹钟！");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    this.notify();
                    try {
                        this.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void run() {
        while(alarmOn)
        {
            Calendar now = Calendar.getInstance();
            int hour = now.get(Calendar.HOUR_OF_DAY);
            int minute = now.get(Calendar.MINUTE);
            int second = now.get(Calendar.SECOND);
            System.out.printf("%d%d:%d%d:%d%d\n",hour/10,hour%10,minute/10,minute%10,second/10,second%10);
            if (hour == alarmHour && minute == alarmMinute && second == 0) {
                setWork(true);
                startAlarm();
            }
            if((minute == alarmHour + 1 && minute == 0 && second == 0)){
                stopAlarm();
            }
        }
    }

    public static void main(String[] args) {
        Calendar now = Calendar.getInstance();
        int hour = now.get(Calendar.HOUR_OF_DAY);
        int minute = now.get(Calendar.MINUTE);
        int second = now.get(Calendar.SECOND);
        Alarm alarm1 = new Alarm();
        Alarm alarm2 = new Alarm(9,0);
        Alarm alarm3 = new Alarm(hour,minute+1,"newAlarm");
        Thread alarmThread = new Thread(alarm3,"newAlarm");
        alarmThread.start();
    }
}
